﻿using DailyApp.WPF.Models;
using DailyApp.WPF.MsgEvents;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace DailyApp.WPF.ViewModels
{
    internal class MainWindowViewModel : BindableBase
    {

        public readonly IEventAggregator eventAggregator;


        /// <summary>
        /// 控制左侧菜单是否出现的属性
        /// </summary>
        private bool _isLeftMenuOpen;

        public bool IsLeftMenuOpen
        {
            get { return _isLeftMenuOpen; }
            set
            {
                _isLeftMenuOpen = value;
                IsToggleButtonChecked = _isLeftMenuOpen;//控制按钮选中状态
                RaisePropertyChanged();
            }
        }


        //控制切换按钮的显示状态，逻辑上与 IsLeftMenuOpen 绑定
        private bool _isToggleButtonChecked;

        public bool IsToggleButtonChecked
        {
            get { return _isToggleButtonChecked; }
            set
            {
                _isToggleButtonChecked = value;
                RaisePropertyChanged();
            }
        }




        /// <summary>
        /// 构造函数
        /// </summary>
        public MainWindowViewModel(IRegionManager _regionManager, IEventAggregator _eventAggregator)
        {
            #region 不太好的选择
            ////使用对象初始化器不太好，这样扩展性就差了
            //leftMenusList=new List<LeftMenu>() 
            //{

            //};
            #endregion

            //获取依赖注入服务
            regionManager = _regionManager;
            eventAggregator = _eventAggregator;

            //委托命令实现
            NavigateCmd = new DelegateCommand<LeftMenuModel>(NavigateFuc);
            ToggleLeftMenuCmd = new DelegateCommand(ToggleLeftMenuFunc);
            MovePrevCmd = new DelegateCommand(MovePreFunc);
            MoveNextCmd = new DelegateCommand(MoveNextFunc);
            HomeCommand = new DelegateCommand(HomeFunc);

            //订阅事件
            eventAggregator.GetEvent<SelectedIndexChangedEvent>()
           .Subscribe(OnSelectedIndexChanged, ThreadOption.UIThread);

            //实例化列表并添加数据            
            CreatMenusList();
        }

        #region 左侧菜单栏属性
        /// <summary>
        /// 左侧菜单列表属性
        /// </summary>
        private List<LeftMenuModel> leftMenusList;

        public List<LeftMenuModel> LeftMenusList
        {
            get { return leftMenusList; }
            set
            {
                leftMenusList = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 左侧菜单栏添加项目的具体方法
        /// </summary>
        private void CreatMenusList()
        {
            LeftMenusList = new List<LeftMenuModel>
            {
                new LeftMenuModel() { Icon = "Home", MenuName = "首页", ViewName = "HomeUC" },//icon使用md中自带的枚举类型即可
                new LeftMenuModel() { Icon = "NotebookQutline", MenuName = "待办事项", ViewName = "WaitUC" },//icon使用md中自带的枚举类型即可
                new LeftMenuModel() { Icon = "NotebookPlus", MenuName = "备忘录", ViewName = "MemoUC" },//icon使用md中自带的枚举类型即可
                new LeftMenuModel() { Icon = "Cog", MenuName = "设置", ViewName = "SettingsUC" }//icon使用md中自带的枚举类型即可
            };
        }
        #endregion


        #region 左侧菜单栏及其导航设置

        //区域管理器
        private readonly IRegionManager regionManager;

        //区域跳转命令
        public DelegateCommand<LeftMenuModel> NavigateCmd { get; set; }

        //切换左侧菜单是否展示的命令
        public DelegateCommand ToggleLeftMenuCmd { get; set; }

        //具体方法--跳转导航
        private void NavigateFuc(LeftMenuModel menu)
        {
            //切换导航区域
            if (menu == null || string.IsNullOrEmpty(menu.ViewName))
                return;

            regionManager.RequestNavigate("MainViewRegion", menu.ViewName, callback =>
            {
                journal = callback.Context.NavigationService.Journal;//将记录放入记录栈中
            });
        }

        /// <summary>
        /// 左侧菜单是否展示
        /// </summary>
        private void ToggleLeftMenuFunc()
        {
            if (IsLeftMenuOpen != null)
            {
                if (IsLeftMenuOpen == false)
                    IsLeftMenuOpen = true;
                else
                    IsLeftMenuOpen = false;
            }
        }

        /// <summary>
        /// 初始化导航页面为首页的方法
        /// </summary>
        public void InitNavigation(string loginName)
        {
            var param = new NavigationParameters
            {
                {"LoginName", loginName}
            };

            //顺道传参
            regionManager.RequestNavigate("MainViewRegion", "HomeUC", callback =>
            {
                journal = callback.Context.NavigationService.Journal;//将记录放入记录栈中
            }, param);
        }

        #endregion


        #region 返回主页

        public DelegateCommand HomeCommand { get; set; }

        private int _mySelectedIndex;

        public int MySelectedIndex
        {
            get { return _mySelectedIndex; }
            set
            {
                _mySelectedIndex = value;
                RaisePropertyChanged();
            }
        }


        private void HomeFunc()
        {
            NavigateFuc(new LeftMenuModel { ViewName = "HomeUC" });

            //处理返回首页时listbox的选项不能指向首页的bug（因为数据源的更新不会影响到前端）
            MySelectedIndex = 0;
            IsLeftMenuOpen = false;
        }

        #endregion

        private void OnSelectedIndexChanged(int newIndex)
        {
            // 更新本地属性
            MySelectedIndex = newIndex;
            IsLeftMenuOpen = false;
        }


        #region 实现前进后退--记录

        //记录历史的栈
        private IRegionNavigationJournal journal;

        //后退的命令
        public DelegateCommand MovePrevCmd { get; private set; }//private set可以让外部无法进行设置。

        //前进命令
        public DelegateCommand MoveNextCmd { get; private set; }

        //具体方法实现--后退
        private void MovePreFunc()
        {
            if (journal != null && journal.CanGoBack)
            {
                journal.GoBack();
            }
            return;
        }

        //前进
        private void MoveNextFunc()
        {
            if (journal != null && journal.CanGoForward)
            {
                journal.GoForward();
            }
            return;
        }

        #endregion


    }
}
